package com.four.reimbursemanage.service.impl;

import com.four.reimbursemanage.common.bean.Page;
import com.four.reimbursemanage.dao.OutmoneyApplyMapper;
import com.four.reimbursemanage.entity.Dailyapply;
import com.four.reimbursemanage.entity.OutmoneyApply;
import com.four.reimbursemanage.service.OutmoneyApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OutmoneyApplyServiceImpl implements OutmoneyApplyService {
    @Autowired
    private OutmoneyApplyMapper outmoneyApplyMapper;
    @Override
    public Page<OutmoneyApply> getOnrePage(int pageNum, int pageSize, String memberName, Integer state) {
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<OutmoneyApply> list=outmoneyApplyMapper.selectOutmoneyApplyList(start,pageSize,memberName,state);
        //获取总条目数
        int total=outmoneyApplyMapper.countOutmoneyApply(memberName,state);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<OutmoneyApply> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    /*查询本人申请*/
    @Override
    public Page<OutmoneyApply> getOnrePageUser(int pageNum, int pageSize, String memberName, Integer state, String number) {
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<OutmoneyApply> list=outmoneyApplyMapper.selectByUser(start,pageSize,memberName,state,number);
        //获取总条目数
        int total=outmoneyApplyMapper.countOutmoneyApplyUser(memberName,state,number);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<OutmoneyApply> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    @Override
    public boolean insertOutmoneyApply(OutmoneyApply outmoneyApply) {
        if (outmoneyApply == null){
            return false;
        }
        return outmoneyApplyMapper.insertOutmoneyApply(outmoneyApply)>0;
    }

    @Override
    public boolean updateOutmoneyApply(OutmoneyApply outmoneyApply) {
        if (outmoneyApply == null) {
            return false;
        }
        return outmoneyApplyMapper.updateOutmoneyApply(outmoneyApply)>0;
    }

    @Override
    public boolean updateC(OutmoneyApply outmoneyApply) {
        if (outmoneyApply == null){
            return false;
        }
        outmoneyApply.setState(3);
        return outmoneyApplyMapper.updateStateById(outmoneyApply)>0;
    }
}
